package com.oracle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oracle.entity.LoginStudent;
import com.oracle.entity.LoginTeacher;
import com.oracle.mapper.StudentMapper;
import com.oracle.mapper.TeacherMapper;
import com.oracle.pojo.Student;
import com.oracle.pojo.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;


@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private StudentMapper studentMapper;
    /**
     * <p>
     *security 安全认证的核心方法，默认调用
     * </p>
     *
     * @author orl
     * @version 1.0
     * @since 2023/4/6 20:07
     */

    @Override
    public UserDetails loadUserByUsername(String userNum) throws UsernameNotFoundException {

        //1、通过账号查询当前用信息

        QueryWrapper<Teacher> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("teach_Num", userNum);//主要这里要和数据库的字段名一致
        Teacher teacher = teacherMapper.selectOne(queryWrapper1);
        if (ObjectUtils.isEmpty(teacher)){
            QueryWrapper<Student> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("stu_Num",userNum);
            Student student = studentMapper.selectOne(queryWrapper2);
            if (ObjectUtils.isEmpty(student)){
                return null;
            }else {
                return new LoginStudent(student);
            }
        }else {
            return new LoginTeacher(teacher);
        }

    }
}
